package com.nengchuan.eic.nceconomy.service;

import com.nengchuan.eic.nceconomy.constant.RoleEnum;
import com.nengchuan.eic.nceconomy.dao.UserRoleMapper;
import com.nengchuan.eic.nceconomy.entity.dto.OrgInfoDTO;
import com.nengchuan.util.NullCheckUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author panwen
 * @description:
 * @create: 2020-05-15 17:55
 * @version: 1.0
 */
@Service
@Slf4j
public class UserRoleServiceImpl implements UserRoleService {
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public List<OrgInfoDTO> fillManager(List<OrgInfoDTO> orgInfoDTOS) {
        if(CollectionUtils.isEmpty(orgInfoDTOS)){
            return Collections.EMPTY_LIST;
        }
        List<Long> orgIds=orgInfoDTOS.stream().map(e->e.getId()).collect(Collectors.toList());
        List<String> roleCodes= Arrays.asList(RoleEnum.ROLE_NCECONOMY_ADMIN.name(),RoleEnum.ROLE_NCECONOMY_MANAGER.name());
        List<OrgInfoDTO.ManagerDTO> managerDTOS=userRoleMapper.getByRoleCodeAndOrgIdIn(orgIds, roleCodes);
        for (OrgInfoDTO orgInfoDTO : orgInfoDTOS) {
            orgInfoDTO.setAdmins(managerDTOS.stream().filter(e->RoleEnum.ROLE_NCECONOMY_ADMIN.name().equals(e.getRoleCode())&&e.getOrgId().equals(orgInfoDTO.getId())).collect(Collectors.toList()));
            orgInfoDTO.setManagers(managerDTOS.stream().filter(e->RoleEnum.ROLE_NCECONOMY_MANAGER.name().equals(e.getRoleCode())&&e.getOrgId().equals(orgInfoDTO.getId())).collect(Collectors.toList()));
        }
        return orgInfoDTOS;
    }
}
